<template>
  <a-modal
      :title="title"
      :width="720"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      :destroyOnClose="true"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
          <a-select
              v-decorator="['type', { rules: [{ required: true, message: '请选择分类类型' }] }]"
              placeholder="请选择类型"
              @change="handleChangeType"
          >
            <a-select-option
                v-for="(item, index) in CategoryType.data"
                :key="index"
                :value="item.key"
            >{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
              v-decorator="['catName', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
              placeholder="请输入分类名称"
          />
        </a-form-item>
        <a-form-item label="上级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tree-select
              v-decorator="['parentNo']"
              :treeData="categoryListTree"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              allowClear
              placeholder="请选择上级分类"
          ></a-tree-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from "lodash";
import * as Api from '@/api/category'
import CategoryModel from '@/common/model/Category'
import {CategoryTypeEnum} from '@/common/enum/product'

export default {
  data() {
    return {
      // 对话框标题
      type: '',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 上级分类列表
      categoryListTree: []
    }
  },
  watch: {
    type(newType) {
      this.getCategoryList(newType)
      // 设置字段parentNo 为null
      const {form: {setFieldsValue}} = this
      setFieldsValue({parentNo: null})
    }
  },
  computed: {
    title() {
      if (this.type) {
        // 如果有类型, 则返回标题
        return `新增商品分类(${CategoryType[this.type].name})`
      }
      return '新增商品分类'
    },
    CategoryType() {
      return CategoryTypeEnum
    }
  },
  methods: {
    // 显示对话框
    add(type) {
      // 设置类型
      this.type = type
      // 显示窗口
      this.visible = true
      // 设置默认值
      this.setFieldsValue(type)
    },

    // 设置默认值
    setFieldsValue(type) {
      const {form: {setFieldsValue}} = this
      // 设置表单内容
      this.$nextTick(() => {
        setFieldsValue(_.pick({type}, ['type']))
      })
    },

    // 获取分类列表
    getCategoryList(type) {
      const self = this
      Api.list({type}).then(result => {
        // 格式化分类列表
        self.categoryListTree = CategoryModel.formatTreeData(result.items)
      })
    },

    // 处理类型变化
    handleChangeType(e) {
      debugger
      this.type = e
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {form: {validateFields}} = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    // 关闭对话框事件
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    // 提交到后端api
    onFormSubmit(values) {
      this.confirmLoading = true
      Api.add(values)
          .then(result => {
            // 显示成功
            this.$message.success('操作成功', 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit', values)
          })
          .finally(result => {
            this.confirmLoading = false
          })
    }
  }
}
</script>
